<template>
  <div>
    <h1>三级分类页面</h1>
    <el-switch
      v-model="draggable"
      active-color="#13ce66"
      inactive-color="#ff4949"
      active-text="开启拖拽"
      inactive-text="关闭拖拽"
    >
    </el-switch>

    <!-- <el-switch v-model="draggable" active-text="开启拖拽" inactive-text="关闭拖拽"></el-switch>
    <el-button v-if="draggable" @click="batchSave">批量保存</el-button>
    <el-button type="danger" @click="batchDelete">批量删除</el-button> -->

    <el-tree
      @node-drop="nodeDrop"
      :allow-drop="allowOrop"
      :draggable="draggable"
      node-key="catId"
      :default-expanded-keys="defaultExpandedKeys"
      :data="data"
      :props="defaultProps"
      :expand-on-click-node="false"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="data.catLevel != 3"
            type="text"
            size="mini"
            @click="
              category = { sort: 0, showStatus: 1 };
              append(data);
            "
          >
            Append 
          </el-button>
          <el-button type="text" size="mini" @click="() => update(data)">
            Update
          </el-button>
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(data)"
          >
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog title="分类编辑" :visible.sync="dialogFormVisible">
      <!-- <h1>{{ category }}</h1> -->
      <el-form :model="category">
        <el-form-item label="分类名称" label-width="210px">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      updateNodes: [],
      level: 1,
      draggable: false,
      countLevel: 1,
      defaultExpandedKeys: [],
      category: {
        sort: 0,
        showStatus: 1,
      },
      dialogFormVisible: false,
      data: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  methods: {
    nodeDrop(draggingNode, dropNode, type) {
      // console.log("拖拽结束", draggingNode, dropNode, type);
      if (type == "before" || type == "after") {
        this.level = dropNode.level;
        this.pid = dropNode.data.parentCid;

        this.collectNodes(dropNode.parent.childNodes);
      } else if (type == "inner") {
        this.pid = dropNode.data.catId;
      }
      console.log("需要修改的对象为:", this.updateNodes);
    },
    collectNodes(nodes) {
      // console.log("需要重新收集的对象为:",nodes);
      for (var i = 0; i < nodes.length; i++) {
        // let obj=[catId:nodes[i].data.catId,parentCid:this.pid,sort:i,catLevel:this.catLevel]
        this.updateNodes.push({
          catId: nodes[i].data.catId,
          parentCid: this.pid,
          sort: i,
          catLevel: this.catLevel,
          name: nodes[i].data.name,
        });
      }
    },
    //allowOrop
    allowOrop(draggingNode, dropNode, type) {
      // console.log(draggingNode, dropNode, type);
      this.getCountLevel(draggingNode);
      // console.log("被拖拽的层数为:", this.countLevel);
      if (type == "next" || type == "prev") {
        if (this.countLevel == 1) return true;
        if (this.countLevel == 2 && dropNode.level != 3) return true;
        if (this.countLevel == 3 && dropNode.level == 1) return true;
      } else if (type == "inner") {
        if (this.countLevel == 1 && dropNode.level != 3) return true;
        if (this.countLevel == 2 && dropNode.level == 1) return true;
      }
      return false;
    },
    getCountLevel(node) {
      this.countLevel = 1;
      if (node.childNodes.length > 0) {
        this.countLevel = 2;
        for (var i in node.childNodes) {
          if (node.childNodes[i].childNodes.length > 0) {
            this.countLevel = 3;
            return;
          }
        }
      }
    },
    remove(node) {
      var ids = [node.catId];
      this.$http({
        url: this.$http.adornUrl(`/product/category/delete`),
        method: "post",
        data: this.$http.adornData(ids, false),
      }).then(({ data }) => {
        console.log(data);
        if (data.code == 0) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getDataList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    update(node, data) {
      this.dialogFormVisible = true;
      console.log(node, data);
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${node.catId}`),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.category = data.category;
      });
    },
    //提价数据
    save() {
      let methodUrl = "save";

      if (this.category.catId) {
        methodUrl = "update";
      }

      this.$http({
        url: this.$http.adornUrl(`/product/category/${methodUrl}`),
        method: "post",
        data: this.$http.adornData(this.category, false),
      }).then(({ data }) => {
        console.log(data);
        if (data.code == 0) {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.dialogFormVisible = false;
          this.getDataList();
          this.defaultExpandedKeys = [this.category.parentCid];
        } else {
          this.$message.error("编辑失败");
        }
      });
    },
    append(node) {
      console.log("弹窗", node);
      this.category.parentCid = node.catId;
      this.category.catLevel = node.catLevel + 1;
      this.dialogFormVisible = true;
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;

      this.$http({
        url: this.$http.adornUrl("/product/category/tree/list"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.data = data.data;
      });
    },
  },
  created() {
    this.getDataList();
  },
};
</script>
